package com.swust.starter;

import com.swust.starter.condition.MyConditionAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(WeatherSource.class) // 让容器知道这个属性类，并将值传入类的属性中
@ConditionalOnProperty(name = "weather.enable", havingValue = "enable") // 自动配置过程中，必须符合里面的内容，否则无法启动
@MyConditionAnnotation(value = "enable") // 自定义condition，交给MyCondition处理，逻辑就是里面的value必须是enable字段
public class WeatherAutoConfiguration {
    @Autowired
    private WeatherSource weatherSource;

    /**
     * 下面也可以直接在WeatherService类上添加注解@Service就可以了
     */
    @Bean
    @ConditionalOnMissingBean(WeatherService.class) // 容器中没有WeatherService实例才创建并注入容器中
    public WeatherService weatherService() {
        return new WeatherService(weatherSource);
    }
}
